import Opt from '@/components/dt/info/Opt.js'
import billMgr from '@/biz/bonus/bill'
import dateOpt from '@/biz/option/date.js'
import nameOpt from '@/biz/option/name.js'
import tool from '@/common/tool'

class RoomColumn extends Opt {

  constructor(name, bonusId, update, type) {
    super()
    this.name = name
    this.type = type
    this.bonusId = bonusId
    this.update = update
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [{
      type: 'index',
      fixed: 'left',
      title: '序号',
      minWidth: 50,
    },
    {
      title: '是否本次结算',
      fixed: 'left',
      minWidth: 110,
      cell: (info) => {
        if (this.name == 'billEdit') {
          return {
            edit: {
              key: 'isParticipate',
              switch: {},
              change: () => {
                clearTimeout(this.timeout)
                billMgr.roomEdit(this.bonusId, info.orderId, info.commissionDetailId,
                  info.isParticipate).then(res => {
                  this.timeout = setTimeout(() => {
                    this.update()
                  }, 1000)
                }).catch(err => {
                  if (err.code != 5001) {
                    info.isParticipate = !info.isParticipate
                  }
                })
              }
            },
          }
        } else {
          return {
            value: (v) => {
              return info.isParticipate ? '本次结算' : '本次不结算'
            },
            text: {
              color: info.isParticipate ? null : 'red'
            }
          }
        }
      }
    },
    {
      field: 'roomName',
      fixed: 'left',
      title: '房源名称',
      minWidth: 130,
      header: {
        filter: {
          title: '房源名称',
          key: 'roomName',
          placeholder: '请输入房源名称',
          input: {},
        },
      },
      cell: (info) => {
        return {
          btn: {
            title: info.roomName,
            event: 'onRoom'
          }
        }
      }
    },
    {
      field: 'subscriptionDate',
      title: '认购日期',
      minWidth: 120,
      header: {
        filter: dateOpt.range({
          title: '认购日期',
          startKey: 'subscriptionStartDate',
          endKey: 'subscriptionEndDate',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (tool.dateDiff(val.subscriptionStartDate, val
                .subscriptionEndDate).Y >= 2) {
              // 日期最大跨度两年
              Message.warning('认购日期跨度至多两年。')
            }
            return !(tool.dateDiff(val.subscriptionStartDate, val
              .subscriptionEndDate).Y >= 2)
          }
        }),
      },
      cell: {
        text: {
          type: 'date'
        }
      }
    },
    {
      field: 'contractDate',
      title: '签约日期',
      minWidth: 120,
      header: {
        filter: dateOpt.range({
          title: '签约日期',
          startKey: 'contractStartDate',
          endKey: 'contractEndDate',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (tool.dateDiff(val.contractStartDate, val
                .contractEndDate).Y >= 2) {
              // 日期最大跨度两年
              Message.warning('认购日期跨度至多两年。')
            }
            return !(tool.dateDiff(val.contractStartDate, val
              .contractEndDate).Y >= 2)
          }
        }),
      },
      cell: {
        text: {
          type: 'date'
        }
      }
    },
    {
      field: 'predictAccountDate',
      title: '全款到账日期',
      minWidth: 140,
      header: {
        filter: dateOpt.range({
          title: '全款到账日期',
          startKey: 'accountDateStart',
          endKey: 'accountDateEnd',
          disabled: (date) => {
            return date.valueOf() > new Date().getTime()
          },
          check: (val) => {
            if (tool.dateDiff(val.accountDateStart, val
                .accountDateEnd).Y >= 2) {
              // 日期最大跨度两年
              Message.warning('全款到账日期跨度至多两年。')
            }
            return !(tool.dateDiff(val.accountDateStart, val
              .accountDateEnd).Y >= 2)
          }
        }),
      },
      cell: {
        text: {
          type: 'date'
        }
      }
    },
    {
      field: 'dealRoomDate',
      title: '交房日期',
      minWidth: 120,
      visible: this.type == 'HANDOVER',
      cell: {
        text: {
          type: 'date'
        }
      }
    },
    {
      field: 'settlementCondition',
      title: '奖金结算条件',
      visible: this.type == 'PRE',
      minWidth: 110,
    },
    {
      field: 'customerName',
      title: '客户姓名',
      minWidth: 110,
      header: {
        filter: {
          title: '客户姓名',
          key: 'customerName',
          placeholder: '请输入客户姓名',
          input: {},
        },
      }
    },
    {
      field: 'area',
      title: '面积(m²)',
      minWidth: 100,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'totalPrice',
      title: '总价(元）',
      minWidth: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'settlementAmount',
      title: '本次结算总销(元)',
      minWidth: 150,
      visible: this.type == 'STANDARD',
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'agentCost',
      title: '本次结算代理费(元)',
      minWidth: 150,
      visible: this.type != 'PRE' && this.type != 'HANDOVER',
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'bonusAmount',
      title: '本次房源奖金(元)',
      minWidth: 150,
      cell: {
        text: {
          type: 'number'
        }
      }
    },
    {
      field: 'contractorName',
      title: '签约人',
      minWidth: 100,
      header: {
        filter: nameOpt.createPushPeople({
          key: 'contractorId',
          title: '签约人',
          to: (info) => {
            console.log(info)
            return {
              contractorId: info.userId,
            }
          }
        })
      }
    },
    {
      field: 'channelSourceName',
      title: '是否渠道',
      minWidth: 130,
    },
    {
      field: 'isAssociates',
      title: '是否关系户',
      minWidth: 100,
    },
    {
      title: '操作',
      fixed: 'right',
      minWidth: 120,
      cell: (info) => {
        return {
          btn: {
            title: '查看奖金明细',
            event: 'onRoom'
          }
        }
      }
    }]
  }
}

export default RoomColumn
